
//标题行多选框单元格渲染器
function  headerCellCheckBoxRender(params) {
    var headerSpan = document.createElement('span');
    headerSpan.innerHTML += "&nbsp;";
    var headerCheckbox = document.createElement('input');
    headerCheckbox.type = "checkbox";
    headerCheckbox.checked = false;
    $(headerCheckbox).on("click", function () {//为列上面的checkbox注册点击事件
        if (this.checked) {
            alert(1)
            params.api.selectAll();
        } else {
            params.api.deselectAll();
        }
    });
    return headerSpan.appendChild(headerCheckbox);
}

//编号单元格渲染器（需要在grid配置信息里面指定formName上下文)
function cellNumRender(params){
    if(!params.context.formName){
        return params.node.id + 1;
    }
    var pageNumber = parseInt($("#"+params.context.formName+ " #pageNumber").val());
    var pageSize = parseInt($("#"+params.context.formName+ " #pageSize").val());
    return (pageNumber-1)*pageSize+params.node.id + 1;
}


//货币格式
function currencyFormatRender(params){
    var num = params.value;
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
            num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
}

//数字多选框渲染器
function numChkCellRender(params){
    var numChk = document.createElement("input");
    numChk.setAttribute("type","checkbox");
    numChk.setAttribute("id",(params.colDef.field+params.rowIndex+"checkboxId"));
    if(params.value == "1") {
        numChk.setAttribute("checked", "checked");
    }
    numChk.addEventListener("change",function(){
        var checked = document.getElementById(((params.colDef.field+params.rowIndex+"checkboxId"))).checked;
        var checked2 =$(this).attr("checked");
        params.value = (checked||checked=="true")?"1":"0";
        params.data[params.colDef.field] = (checked||checked=="true")?"1":"0";
        gridOptions.api.refreshView();
    });
    return numChk;
}

//字典下拉框  DICT = 字典集合
function customEditor(DICT,params){

    var editing = false;
    var eCell = document.createElement('span');
//        var eLabel = document.createTextNode(!params.value?"请选择": DICT[params.value]);
//        var eLabel = document.createElement('span');
    var eLabel = document.createTextNode( DICT[params.value]);
    eCell.appendChild(eLabel);
    var eSelect = document.createElement("select");
//        DICT[""]="请选择";
//        "#unt002" + selectedNodes[i]
    var unt002Id ="unt002"+ params.data.unt008;
    //eSelect.id = unt002Id;
//        eSelect.setAttribute("id", unt002Id);
    $.each(DICT,function(name,value) {
        var eOption = document.createElement("option");
        eOption.setAttribute("value", name);
        if(params.value==name) {
            $("#"+unt002Id).val(name);
            eOption.setAttribute("selected", "selected");
        }
        eOption.innerHTML = value;
        eSelect.appendChild(eOption);
    });
    eSelect.value = params.value;
    eSelect.style.display="none";
    eCell.appendChild(eSelect);
    eCell.addEventListener('click', function () {
        if (!editing) {
//                eLabel.style.display="none";
            eSelect.style.display="block";
            eCell.removeChild(eLabel);
//                eCell.appendChild(eSelect);
            eSelect.focus();
            editing = true;
        }
    });

    eSelect.addEventListener('blur', function () {
        if (editing) {
            editing = false;
            eSelect.style.display="none";
//                eLabel.style.display="block";
//                eCell.removeChild(eSelect);
            eCell.appendChild(eLabel);
        }
    });

    eSelect.addEventListener('change', function (){

        if (editing) {

            editing = false;
            var newValue = eSelect.value;
            params.data[params.colDef.field] = newValue;
            $("#"+unt002Id).val(newValue);
            eLabel.nodeValue = DICT[newValue];
            eSelect.style.display="none";
//                eLabel.style.display="block";
//                eCell.removeChild(eSelect);
            eCell.appendChild(eLabel);
//                updateDocTemplate(params.colDef.colId,newValue,params.data.DICT);
        }
    });
    return eCell;
}